using System;
using System.Collections.Generic;
using System.Text;

namespace Schemata.Core
{
    /// <summary>
    /// Represents a table.
    /// </summary>
    public class TableSchema : GenericSchema
    {
        private DatabaseSchema _database;

        /// <summary>
        /// Gets or sets the database.
        /// </summary>
        /// <value>The database.</value>
        public DatabaseSchema Database
        {
            get { return _database; }
            set { _database = value; }
        }

        /// <summary>
        /// Gets the primary key columns.
        /// </summary>
        /// <value>The primary key columns.</value>
        public List<ColumnSchema> PrimaryKeyColumns
        {
            get 
            {
                List<ColumnSchema> list = new List<ColumnSchema>();

                foreach (ColumnSchema columnSchema in Columns)
                {
                    if (columnSchema.IsPrimaryKeyMember)
                    {
                        list.Add(columnSchema);
                    }
                }

                return list;
            }
        }

        /// <summary>
        /// Gets the foreign keys.
        /// </summary>
        /// <value>The foreign keys.</value>
        public List<TableKeySchema> ForeignKeys
        {
            get { return Database.Provider.GetForeignKeys(this); }
        }

        /// <summary>
        /// Gets the name of the column schema by.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public ColumnSchema GetColumnSchemaByName(string name )
        {
            ColumnSchema result = null;

            foreach (ColumnSchema columnSchema in Columns)
            {
                if (columnSchema.Name == name)
                {
                    result = columnSchema;
                    break;
                }
            }

            return result;
        }

        /// <summary>
        /// Gets the columns.
        /// </summary>
        /// <value>The columns.</value>
        public List<ColumnSchema> Columns
        {
            get { return Database.Provider.GetColumns(this); }
        }
    }
}
